package demo.xml.jaxen;

import java.util.List;

import org.jaxen.javabean.JavaBeanXPath;
import org.jaxen.saxpath.helpers.XPathReaderFactory;
import org.junit.jupiter.api.Assertions;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

public class JavaBeanNavigator {
    private void run() throws Exception {
        Person bob = new Person( "bob", 30 );
        bob.addBrother( new Person( "billy", 34 ) );
        bob.addBrother( new Person( "seth", 29 ) );
        bob.addBrother( new Person( "dave", 32 ) );
        bob.addBrother( new Person( "jim", 29 ) );
        bob.addBrother( new Person( "larry", 42 ) );
        bob.addBrother( new Person( "ted", 22 ) );
        
        System.setProperty( XPathReaderFactory.DRIVER_PROPERTY, "" );
        JavaBeanXPath xpath = new JavaBeanXPath( "brother[position()<4]/name" );
        List result = (List) xpath.evaluate( bob );
        Assertions.assertEquals(3, result.size());
        
        xpath = new JavaBeanXPath( "./brothers[name='dave']" );
        Object object = xpath.evaluate( bob );
        ConsoleUtils.log("clazz", object.getClass(), "object", object);
        if(object instanceof List) {
            List items = (List)object;
            ConsoleUtils.log("items.size", items.size());
            for (int i = 0; i < items.size(); i++) {
                Object item = items.get(i);
                ConsoleUtils.log(Integer.toString(i), "clazz", item.getClass(), "item", item);
            }
        }
    }    
    
    
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            JavaBeanNavigator test = new JavaBeanNavigator();
            test.run();
        } catch (Exception e) {
            e.printStackTrace();
            timer.print("执行总用时");
        }
    }
}
